package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.service.ex.*;
import com.cy.store.utils.JsonResult;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * 控制层类的基类
 */
public class BaseController {

    public static final int OK = 200; //操作成功的状态码

    //请求处理方法，这个方法的返回值就是需要传递给前端的数据
    //自动将异常对象传递给此方法的参数列表上
    @ExceptionHandler({ServiceException.class,FileUploadException.class})  //用于统一处理抛出的异常
    public JsonResult<Void> handlerException(Throwable e){
        JsonResult<Void> jsonResult = new JsonResult<Void>(e);
        if (e instanceof UserNameDuplicatedException){
            jsonResult.setState(4000);
            jsonResult.setMessage("用户名已被占用！");
        } else if (e instanceof UserNotFoundException){
            jsonResult.setState(4001);
            jsonResult.setMessage("用户数据不存在异常！");
        }else if (e instanceof PasswordNotMatchException) {
            jsonResult.setState(4002);
            jsonResult.setMessage("用户名密码错误异常！");
        }else if (e instanceof AddressCountLimitException){
            jsonResult.setState(4003);
            jsonResult.setMessage("收货地址数量超出上限异常！");
        } else if (e instanceof AddressNotFoundException) {
            jsonResult.setState(4004);
            jsonResult.setMessage("用户收获地址不存在异常！");
        } else if (e instanceof AccessDeniedException) {
            jsonResult.setState(4005);
            jsonResult.setMessage("用户访问异常！");
        }else if (e instanceof ProductNotFoundException) {
            jsonResult.setState(4006);
            jsonResult.setMessage("商品数据访问异常！");
        } else if (e instanceof CartNotFoundException) {
            jsonResult.setState(4007);
            jsonResult.setMessage("购物车数据不存在异常！");
        } else if (e instanceof InsertException){
            jsonResult.setState(5000);
            jsonResult.setMessage("注册时产生未知异常！");
        } else if (e instanceof DeleteException){
            jsonResult.setState(5002);
            jsonResult.setMessage("删除数据时产生未知异常！");
        }else if (e instanceof Update){
            jsonResult.setState(5003);
            jsonResult.setMessage("更新数据产生未知异常！");
        } else if (e instanceof FileEmptyException) {
            jsonResult.setState(6000);
        } else if (e instanceof FileSizeException) {
            jsonResult.setState(6001);
        } else if (e instanceof FileTypeException) {
            jsonResult.setState(6002);
        } else if (e instanceof FileStateException) {
            jsonResult.setState(6003);
        } else if (e instanceof FileUploadIOException) {
            jsonResult.setState(6004);
        }
        return jsonResult;
    }

    /**
     * 获取session对象中的uid
     * @param session
     * @return  当前登录对象的uid
     */
    protected final Integer getUidFromSession(HttpSession session){
       return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * 获取session对象中的username
     * @param session
     * @return  当前登录对象的username
     */
    protected final String getUserNameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }
}
